package com.twu.biblioteca.menu;


import com.twu.biblioteca.Librarian;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class LibraryMenu implements Menu {
    public static final String RETURN_MENU = "ReturnMenu";
    public static final String CHECKOUT_MENU = "CheckoutMenu";
    private Librarian librarian;
    private static Map<String, String> optionMap = new HashMap<String, String>();

    public LibraryMenu(Librarian librarian) {
        this.librarian = librarian;
        this.showMenu();
    }

    static {
        optionMap.put("A","listBooks");
        optionMap.put("B","listMovies");
        optionMap.put("C","checkout");
        optionMap.put("D","returnBook");
        optionMap.put("E","showMyProfile");
        optionMap.put("F","exit");
    }

    @Override
    public void showMenu() {
        System.out.println("========================================");
        System.out.println("Welcome to Biblioteca!\n\t-----The Bangalore Public Library!");
        System.out.println("========================================");
        System.out.println("->A.List Books");
        System.out.println("->B.List Movies");
        System.out.println("->C.Checkout");
        System.out.println("->D.Return");
        System.out.println("->E.My Profile");
        System.out.println("->F.Exit");
        System.out.println("Please choose an option(enter an index):");
    }

    @Override
    public Menu processInput(String chosenOption) {
        Method method;
        String methodName = LibraryMenu.optionMap.get(chosenOption + "");
        if (methodName == null) {
            System.out.println("Select an invalid option!Please enter a number between 1-5");
            return this;
        }
        try {
            method = this.getClass().getMethod(methodName);
            return (Menu) method.invoke(this);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Menu listBooks() {
        System.out.println("Here is the list of all books:");
        librarian.listBooks();
        return this;
    }

    public Menu listMovies() {
        System.out.println("Here is the list of all movies:");
        librarian.listMovies();
        return this;
    }


    public Menu checkout() {
        if (!librarian.hasLogIn) {
            LoginMenu loginMenu = new LoginMenu(librarian);
            loginMenu.parentMenu = CHECKOUT_MENU;
            return loginMenu;
        }
        else
            return new CheckoutMenu(librarian);
    }

    public Menu returnBook() {
        if (!librarian.hasLogIn) {
            LoginMenu loginMenu = new LoginMenu(librarian);
            loginMenu.parentMenu = RETURN_MENU;
            return loginMenu;
        }
        else
            return new ReturnMenu(librarian);
    }

    public Menu showMyProfile() {
        if(librarian.hasLogIn) {
            System.out.println("*******My Profile*******");
            System.out.println(librarian.currentUser);
            System.out.println("\n\n\n");
            return this;
        }
        else {
            System.out.println("Sorry.You haven't logged in.");
            return new LoginMenu(librarian);

        }
    }

    public void exit() {
        System.out.println("Exit now. See you!");
        System.exit(0);
    }


}
